package tv.accedo.memory.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tv.accedo.memory.dao.AchiDao;
import tv.accedo.memory.dao.UserDao;

public class AchiServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8"); 
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter(); 
		System.out.println("Server get one request from client side!"); 
		
		
		String achi_sn = request.getParameter("achi_sn"); 
		String username = request.getParameter("username");
		
		UserDao userDao = new UserDao();
		AchiDao achiDao = new AchiDao(); 
		Boolean flag = userDao.addAchiToUser(achi_sn, username); 
		
		if (flag == true) {
			String card_sn = achiDao.getCardsnByAchisn(achi_sn); 
			System.out.println(card_sn); 
			String jsonString = "{pid:'" + card_sn + "',epname:'boliang'}";
			out.print(jsonString); 
		} else {
			out.print("{pid:WRONG,epname:'boliang'}");
		}	 
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}

